import org.locationtech.jts.io.ParseException;

import java.io.File;
import java.security.MessageDigest;
import java.util.ArrayList;

public class Main {
    private static Benchmark benchmark;
    //    private static long[] countArray = {1000, 2000, 5000, 10000, 20000};
    private static long[] countArray = {1};

    public static void main(String[] args) throws ParseException {
        File networkPath = new File(Main.class.getResource("/network-config3.yaml").getFile());

        String channelName = "txchannel";
        benchmark = new Benchmark(channelName, networkPath);
        String chaincodeName = "endorsercc";
        benchmark.setChaincodeName(chaincodeName);
//        benchmark.installChaincode();
//        readTPSGeoData(Main.class.getResource("/BL/BL.shp").getFile());


        for (long count : countArray) {
            System.out.println("===>交易次数： " + count);
            benchmark.setTxCount(count);
//            writeTPS(count);
            readTPS(count);
            System.out.println(count);
        }
    }

    private static void writeTPS(long count) {
        double totalTime;
        String chaincodeName = "endorsercc";
        String functionName = "PutRecord";
        String[] chaincodeArgs = {"keye", "valuee"};
        benchmark.setFunctionName(functionName);
        benchmark.setChaincodeName(chaincodeName);
        benchmark.setArgsStr(chaincodeArgs);
        totalTime = benchmark.calcWriteStrTPS();

        System.out.println("写入总时间： " + totalTime + "s" + "，TPS： " + count / totalTime);
    }


    private static void readTPS(long count) {
        double totalTime;
        String chaincodeName = "endorsercc";
        String functionName = "GetRecordByKey";
        String[] chaincodeArgs = {"key2"};
        benchmark.setFunctionName(functionName);
        benchmark.setChaincodeName(chaincodeName);
        benchmark.setArgsStr(chaincodeArgs);
        totalTime = benchmark.calcReadTPS();

        System.out.println("查询总时间： " + totalTime + "s" + "，TPS： " + count / totalTime);
    }



    private void writeTPSGeoData(String shpPath) {
        try {
            ArrayList<byte[]> arrayList = new GeoData().getGeo(shpPath);

            double totalTime = 0;
            String chaincodeName = "endorsercc";
            String functionName = "PutRecord";
            String hash = getSHA256("key2");
            byte[][] chaincodeArgs = null;

            benchmark.setFunctionName(functionName);
            benchmark.setChaincodeName(chaincodeName);
            int i = 0;
            for (byte[] bytes : arrayList) {
                chaincodeArgs = new byte[][]{(hash + i).getBytes(), bytes};
                benchmark.setArgsBytes(chaincodeArgs);

                totalTime += benchmark.calcWriteByteTPS();

                i++;
                System.out.println(i);
            }
            System.out.println("写入总时间： " + totalTime + "s" + "，TPS： " + arrayList.size() / totalTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    private static void readTPSGeoData(String shpPath) throws ParseException {
        double totalTime = 0;
        ArrayList<byte[]> arrayList = new GeoData().getGeo(shpPath);
        String chaincodeName = "endorsercc";
        String functionName = "GetRecordByKey";
        byte[][] chaincodeArgs = null;
        String hash = getSHA256("key2");
        benchmark.setFunctionName(functionName);
        benchmark.setChaincodeName(chaincodeName);
        for (int i = 0; i < arrayList.size(); i++) {
            chaincodeArgs = new byte[][]{(hash + i).getBytes()};
            benchmark.setArgsBytes(chaincodeArgs);
            totalTime += benchmark.calcReadByteTPS();
            System.out.println(i);
        }

        System.out.println("查询总时间： " + totalTime + "s" + "，TPS： " + arrayList.size() / totalTime);
    }

    public static String getSHA256(String str) {
        if (str == null) {
            return null;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.reset();
            messageDigest.update(str.getBytes());
            return byte2Hex(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // StringBuffer 建立的字符串可以进行修改，并且不产生新的未使用对象
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}
